/// 活动参与者数据模型
class ActivityParticipant {
  final int id;
  final int activityId;
  final int userId;
  final String joinTime;
  final List<String> participantStatus;
  final List<String> status;
  final Map<String, dynamic> extraData;
  final String createdAt;
  final String updatedAt;
  final String username;
  final String userAvatarUrl;
  final String userSignature;

  ActivityParticipant({
    required this.id,
    required this.activityId,
    required this.userId,
    required this.joinTime,
    required this.participantStatus,
    required this.status,
    required this.extraData,
    required this.createdAt,
    required this.updatedAt,
    required this.username,
    required this.userAvatarUrl,
    required this.userSignature,
  });

  factory ActivityParticipant.fromJson(Map<String, dynamic> json) {
    return ActivityParticipant(
      id: json['id'] ?? 0,
      activityId: json['activityId'] ?? 0,
      userId: json['userId'] ?? 0,
      joinTime: json['joinTime'] ?? '',
      participantStatus: json['participantStatus'] != null
          ? List<String>.from(json['participantStatus'])
          : [],
      status: json['status'] != null
          ? List<String>.from(json['status'])
          : [],
      extraData: json['extraData'] ?? {},
      createdAt: json['created_at'] ?? '',
      updatedAt: json['updated_at'] ?? '',
      username: json['username'] ?? '',
      userAvatarUrl: json['userAvatarUrl'] ?? '',
      userSignature: json['userSignature'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'activityId': activityId,
      'userId': userId,
      'joinTime': joinTime,
      'participantStatus': participantStatus,
      'status': status,
      'extraData': extraData,
      'created_at': createdAt,
      'updated_at': updatedAt,
      'username': username,
      'userAvatarUrl': userAvatarUrl,
      'userSignature': userSignature,
    };
  }
}

/// 活动参与者查询请求模型
class ActivityParticipantsRequest {
  final int activityId;

  ActivityParticipantsRequest({
    required this.activityId,
  });

  Map<String, dynamic> toJson() {
    return {
      'activity_id': activityId, // 使用下划线格式，和MyApplication2保持一致
    };
  }
}

/// 活动参与者查询响应模型
class ActivityParticipantsResponse {
  final String status;
  final String message;
  final ActivityParticipantsData? data;

  ActivityParticipantsResponse({
    required this.status,
    required this.message,
    this.data,
  });

  bool get isSuccess => status == 'success';

  factory ActivityParticipantsResponse.fromJson(Map<String, dynamic> json) {
    return ActivityParticipantsResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '未知错误',
      data: json['data'] != null 
          ? ActivityParticipantsData.fromJson(json['data']) 
          : null,
    );
  }
}

/// 活动参与者数据
class ActivityParticipantsData {
  final int total;
  final List<ActivityParticipant> participants;

  ActivityParticipantsData({
    required this.total,
    required this.participants,
  });

  factory ActivityParticipantsData.fromJson(Map<String, dynamic> json) {
    return ActivityParticipantsData(
      total: json['total'] ?? 0,
      participants: json['participants'] != null
          ? (json['participants'] as List)
              .map((item) => ActivityParticipant.fromJson(item))
              .toList()
          : [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'total': total,
      'participants': participants.map((p) => p.toJson()).toList(),
    };
  }
}

/// 发布者活动参与者查询请求模型
class PublisherParticipantsRequest {
  final int publisherId;

  PublisherParticipantsRequest({
    required this.publisherId,
  });

  Map<String, dynamic> toJson() {
    return {
      'userid': publisherId.toString(), // 使用userid参数名，与MyApplication2保持一致
    };
  }
}

/// 删除参与者请求模型
class DeleteParticipantRequest {
  final int activityId;
  final int userId;

  DeleteParticipantRequest({
    required this.activityId,
    required this.userId,
  });

  Map<String, dynamic> toJson() {
    return {
      'activityId': activityId,
      'userId': userId,
    };
  }
}

/// 删除参与者响应模型
class DeleteParticipantResponse {
  final String status;
  final String message;

  DeleteParticipantResponse({
    required this.status,
    required this.message,
  });

  bool get isSuccess => status == 'success';

  factory DeleteParticipantResponse.fromJson(Map<String, dynamic> json) {
    return DeleteParticipantResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '未知错误',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
    };
  }
}

/// 加入活动请求模型（严格按照MyApplication2和接口文档2.5实现）
class JoinActivityRequest {
  final int activityId;
  final int userId;
  final String userNickname;
  final String? userAvatarUrl;
  final String? userSignature;
  final List<String>? userTags;
  final int? userPoints;
  final String? participantStatus;
  final Map<String, dynamic>? extraData;

  JoinActivityRequest({
    required this.activityId,
    required this.userId,
    required this.userNickname,
    this.userAvatarUrl,
    this.userSignature,
    this.userTags,
    this.userPoints,
    this.participantStatus,
    this.extraData,
  });

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{
      'activityId': activityId,
      'userId': userId,
      'userNickname': userNickname,
    };

    // 添加可选参数
    if (userAvatarUrl != null) json['userAvatarUrl'] = userAvatarUrl;
    if (userSignature != null) json['userSignature'] = userSignature;
    if (userTags != null) json['userTags'] = userTags;
    if (userPoints != null) json['userPoints'] = userPoints;
    if (participantStatus != null) json['participantStatus'] = participantStatus;
    if (extraData != null) json['extraData'] = extraData;

    return json;
  }
}

/// 加入活动响应模型
class JoinActivityResponse {
  final String status;
  final String message;

  JoinActivityResponse({
    required this.status,
    required this.message,
  });

  bool get isSuccess => status == 'success';

  factory JoinActivityResponse.fromJson(Map<String, dynamic> json) {
    return JoinActivityResponse(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '未知错误',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
    };
  }
}